package com.hwhhhh.mygrad.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.utils.StringUtils;

public class HintFragment extends DialogFragment {
    public static final String TITLE = "title";
    public static final String CONTENT = "content";
    public static final String POSITIVE = "positive";
    public static final String NEGATIVE = "negative";
    public static final String REQUEST_CODE = "request_code";

    private View rootView;
    private TextView tvTitle, tvContent, tvPositive, tvNegative;
    private Bundle bundle;

    /**
     * 新建一个提示框
     * @param title 提示标题，不可为空
     * @param content   提示的内容，不可为空
     * @param positive  提示的确定按键名，默认为确定，可为空
     * @param negative  提示的取消按键名，默认为取消，可为空
     * @param requestCode   请求码，用于识别是哪个对话框发来的请求，不可为空
     * @return
     */
    public static HintFragment newInstance(String title, String content, String positive, String negative, int requestCode) {
        HintFragment frag = new HintFragment();
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        args.putString(CONTENT, content);
        if (!StringUtils.isEmpty(positive)) args.putString(POSITIVE, positive);
        else args.putString(POSITIVE, "确定");
        if (!StringUtils.isEmpty(negative)) args.putString(NEGATIVE, negative);
        else args.putString(NEGATIVE, "取消");
        args.putInt(REQUEST_CODE, requestCode);
        frag.setArguments(args);
        return frag;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_hint, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        tvTitle = rootView.findViewById(R.id.hint_title);
        tvContent = rootView.findViewById(R.id.hint_content);
        tvPositive = rootView.findViewById(R.id.hint_positive);
        tvNegative = rootView.findViewById(R.id.hint_negative);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        String title = getArguments().getString(TITLE);
        String content = getArguments().getString(CONTENT);
        String positive = getArguments().getString(POSITIVE);
        String negative = getArguments().getString(NEGATIVE);
        int requestCode = getArguments().getInt(REQUEST_CODE);
        tvTitle.setText(title);
        tvContent.setText(content);
        tvPositive.setText(positive);
        tvNegative.setText(negative);
        tvPositive.setOnClickListener(v -> {
            ((HintCallBack)getActivity()).doPositive(requestCode);
        });
        tvNegative.setOnClickListener(v -> {
            ((HintCallBack)getActivity()).doNegative(requestCode);
        });
    }

    @Override
    public void onStart() {
        // 设置对话框的尺寸
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(params);
        // 设置背景透明，使背景的圆角显现
        getDialog().getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT));
        super.onStart();
    }

    public interface HintCallBack {
        void doPositive(int requestCode);
        void doNegative(int requestCode);
    }

    public void setBundle(Bundle bundle) {
        this.bundle = bundle;
    }

    public Bundle getBundle() {
        return this.bundle;
    }
}
